package com.run.motion.controller;

import com.run.motion.config.IpConfig;
import com.run.motion.entity.FoodDetail;
import com.run.motion.service.IFoodDetailService;
import com.run.motion.util.page.AjaxResult;
import com.run.motion.util.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * @author tyx-qchk
 * @date 2022/12/6 14:06
 */
@Api(tags = "食物详情")
@RestController
@RequestMapping("/food/detail")
public class FoodDetailController extends BaseController{
    @Autowired
    IFoodDetailService foodDetailService;
    @Autowired
    private IpConfig ipConfig;
    /**
     * 查询食物详情列表
     */
    @ApiOperation(value = "查询食物详情列表")
    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        List<FoodDetail> list = foodDetailService.selectFoodDetailList();
        return getDataTable(list);
    }

    /**
     * 根据二级分类id查询食物详情列表
     */
    @ApiOperation(value = "根据二级分类id查询食物详情列表")
    @GetMapping("/list/{typeId}")
    public TableDataInfo byTypeIdList(@PathVariable("typeId") String typeId)
    {
        startPage();
        List<FoodDetail> list = foodDetailService.selectFoodDetailByTypeIdlList(typeId);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setImgUrl(ipConfig.getIp()+list.get(i).getImgUrl());
        }
        return getDataTable(list);
    }



    /**
     * 获取食物详情详细信息(根据id查)
     */
    @ApiOperation(value = "获取食物详情详细信息(根据id查)")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(foodDetailService.selectFoodDetailById(id));
    }

    /**
     * 获取食物详情详细信息(根据食物名模糊查询)
     */
    @ApiOperation(value = "获取食物详情详细信息(根据食物名模糊查询)")
    @GetMapping(value = "/foodName")
    public AjaxResult getLinkInfo(@RequestParam("foodName") String foodName)
    {
        List<FoodDetail> list = foodDetailService.selectFoodDetailByFoodName(foodName);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setImgUrl(ipConfig.getIp()+list.get(i).getImgUrl());
        }
        return AjaxResult.success(list);
    }

    /**
     * 新增食物详情
     */
    @ApiOperation(value = "新增食物详情")
    @PostMapping
    public AjaxResult add(@RequestBody FoodDetail foodDetail)
    {
//        生成uuid
        String id = String.valueOf(UUID.randomUUID());
        foodDetail.setId(id);
        return toAjax(foodDetailService.insertFoodDetail(foodDetail));
    }

    /**
     * 修改食物详情
     */
    @ApiOperation(value = "修改食物详情")
    @PutMapping
    public AjaxResult edit(@RequestBody FoodDetail foodDetail)
    {
        return toAjax(foodDetailService.updateFoodDetail(foodDetail));
    }

    /**
     * 删除食物详情
     */
    @ApiOperation(value = "删除食物详情")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(foodDetailService.deleteFoodDetailByIds(ids));
    }
}
